package com.md24housecall.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.md24housecall.activity.R;
import com.md24housecall.manager.DimenManager;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;
import com.md24housecall.util.Utils;
import com.md24housecall.view.RatioImageView;
import com.md24housecall.view.RatioRelativeLayout;

public final class HelpFragment extends HomeFragment {
	private static final String KEY_TAB_POSITION = "TestFragment:Position";
	private Utils util = Utils.getInstance();
	private RatioImageView btnSupportEmail;
	private RatioImageView btnSupportPhone;
	private RatioImageView btnRequestCode;
	private final int CLICK_DURATION=1500;

	public static HelpFragment newInstance(int tabPosition) {
		HelpFragment fragment = new HelpFragment();

		Bundle args = new Bundle();
		args.putInt(KEY_TAB_POSITION, tabPosition);
		fragment.setArguments(args);

		return fragment;
	}

	private int mTabPosition = 0;
	private RatioImageView mImgHeader;
	private RatioRelativeLayout mHeaderContainer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments() != null) {
			mTabPosition = getArguments().getInt(KEY_TAB_POSITION, 0);
		}
	}

	private void initialize() {
		mImgHeader = (RatioImageView) mainView.findViewById(R.id.imgHeader);
		mImgHeader.setRatio(DimenManager.WIDTH_TAB_HEADER_IMG,
				DimenManager.HEIGHT_TAB_HEADER_IMG);
		mHeaderContainer = (RatioRelativeLayout) mainView
				.findViewById(R.id.header_container);
		mHeaderContainer.setRatio(DimenManager.WIDTH_TAB_HEADER_IMG,
				DimenManager.HEIGHT_TAB_HEADER_IMG);
		btnRequestCode =(RatioImageView)mainView.findViewById(R.id.btn_howtorequestcode);
		btnRequestCode.setRatio(DimenManager.WIDTH_BUTTON_IMG,DimenManager.HEIGHT_BUTTON_IMG);
		btnSupportEmail =(RatioImageView)mainView.findViewById(R.id.btnSupportEmail);
		btnSupportEmail.setRatio(DimenManager.WIDTH_BUTTON_IMG,DimenManager.HEIGHT_BUTTON_IMG);
		btnSupportPhone =(RatioImageView) mainView.findViewById(R.id.btnSupportPhone);
		btnSupportPhone.setRatio(DimenManager.WIDTH_BUTTON_IMG,DimenManager.HEIGHT_BUTTON_IMG);
	}

	private void addActions() {
		btnSupportEmail.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				endClick=System.currentTimeMillis();
				if(endClick-startClick>=CLICK_DURATION){
					util.sendEmail(getActivity());
					startClick=endClick;
				}
			}
		});

		btnSupportPhone.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				endClick=System.currentTimeMillis();
				if(endClick-startClick>=CLICK_DURATION){
					util.call(getActivity());
					startClick=endClick;
				}
			}
		});
	}

	@Override
	protected View initView(LayoutInflater inflater,ViewGroup container) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.help, container, false);
	}
	
	@Override
	protected void initLayout() {
		// TODO Auto-generated method stub
		try{
			initialize();
			addActions();
		}catch (Exception e) {
			// TODO: handle exception
//			e.printStackTrace();
			MyLog.d(GlobalConstant.TAG, "exception onCreateView HelpFragment: "+e.getMessage());
		}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(KEY_TAB_POSITION, mTabPosition);
	}
}
